package server.java.autoduel.Network;

import java.nio.ByteBuffer;

/**
 * This is a common class for manipulating packets
 * 
 * @author Ian Axelrod
 * @author Liz Ha
 * @author Andrew Muldowney
 * @author Edward Futch
 */
public class Util {
	/**
	 * The delimiter used to signify the end of a packet
	 */
	public static final byte PKT_DLM = '%';

	/**
	 * Takes a packet and converts the data represented into a string until it
	 * hits the defined delimiter
	 * 
	 * @param packet
	 *            the packet to parse
	 * @return the resultant string
	 */
	public static String makeString(ByteBuffer packet) {
		final StringBuilder str = new StringBuilder();
		byte b = packet.get();

		while (b != Util.PKT_DLM) {
			str.append((char) b);
			b = packet.get();
		}
		return str.toString();
	}

}
